import { defineStore } from 'pinia'
import { getMedicineList,deleteMedicine,addMedicine,saveDrugGroup,getDrugDetailById,saveDrugChange } from '@/api/medicineHttp'
import { showLoading } from '../utils/loading';

// 所有药品
export const useMedicineStore = defineStore('medicine', {
  state: () => ({
    medicines: [],
    pageSize: 5,
    hasMore: true,
    isLoading: false,
    medicineDetail:{}
  }),
  
  getters: {
    
  },
  
  actions: {
    // 获取所有列表
    async getMedicineList(page) {
          const res = await getMedicineList(page)
          if (res && res.data) {
            page == 1 ? this.medicines = res.data.data.records : this.medicines = [...this.medicines, ...res.data.data.records]
            this.total = res.data.data.total;
            this.isLoading = false
        }
  },
  // 获取列表详情
    async getDrugDetailById(id){
      const res = await getDrugDetailById(id)
      this.medicineDetail = res.data
    },

    async deleteMedicine(id){
      await deleteMedicine(id)
    },
    async addMedicine(medicine){
      await addMedicine(medicine)
    },
    async saveDrugGroup(group){
      await saveDrugGroup(group)
    },
    async saveDrugChange(drug){
      await saveDrugChange(drug)
    }
  }
}) 

// 获取药品详